﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Models;

public partial class Services_saveModel : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "text/plain";
        Response.Charset = "utf-8";
        int _distid = 0;
        DateTime _date ;
        string _typename = "";
        float _intercept = 0;
        float _coefficient = 0;
        float _goodness = 0;
        string _x_y = "";
        string _x_type="";
        string _y_type="";
        int _rootid = 0;
        int _modelid = 0;
        string _model = "";
        _distid =Convert.ToInt32(Request.QueryString[0].Trim().ToString());
        string x = Request.QueryString[1].Trim().ToString();
        _date =Convert.ToDateTime( x.Replace('_', '-'));
        _typename = Request.QueryString[2].Trim().ToString();
        double i = Convert.ToDouble(Request.QueryString[3].Trim().ToString());
        _intercept =(float) Math.Round(i, 4);
        i = Convert.ToDouble(Request.QueryString[4].Trim().ToString());
        _coefficient=(float)Math.Round(i,4);
        i = Convert.ToDouble(Request.QueryString[5].Trim().ToString());
        _goodness = (float)Math.Round(i, 4);
        _x_y = Request.QueryString[6].Trim().ToString();
        _rootid =Convert.ToInt32(Request.QueryString[7].Trim().ToString());
        _model =Request.QueryString[8].Trim().ToString();
        //switch (_modelid)
        //{
        //    default:
        //    case 0: _model = "GrowingStatus";
        //        break;
        //    case 1: _model = "RsYield";
        //        break;
        //    case 2: _model = "AgriYield";
        //        break;
        //    case 3: _model = "QualityRsForcast";
        //        break;
        //    case 4: _model = "QualityAgriForcast";
        //        break;
        //    case 5: _model = "QualityRsMonitor";
        //        break;
        //    case 6: _model = "DiseaseMonitor";
        //        break;
        //    case 7: _model = "DroughtyMonitor";
        //        break;
        //    case 8: _model = "NutrientMonitor";
        //        break;
        //    case 9: _model = "MoistureMonitor";
        //        break;
        //    case 10: _model = "OptimalCultivate";
        //        break;
        //}
        if (_model == "Disease")
        {
            _modelid = 6;
        }
        else if (_model == "drought")
        {
            _modelid = 7;
        }
        else
        {
            _modelid = 0;
        }

        string[] arr = _x_y.Split('-');
        _x_type = arr[1].Trim().ToString();
        _y_type = arr[0].Trim().ToString();
        string resultJson = "";
        ModelEquations me;
        me = ModelEqManager.GetModel(_distid.ToString(), _date.ToShortDateString(), _typename.ToString(), _x_type, _y_type);
        if (me == null)
        {
            me = new ModelEquations(_distid, _date, _modelid, _model, _typename, _goodness, _intercept, _coefficient, _x_type, _y_type, _rootid);
            int count = ModelEqManager.SaveModel(me);
            if (count > 0)  resultJson = "{\"result\":\"1\"}";
            else resultJson = "{\"result\":\"0\"}";
        }
        else
        {
            resultJson = "{\"result\":\"2\"}";
        }
        //ModelEquations me = new ModelEquations(_distid,_date,_modelid, _model, _typename,_goodness,_intercept,_coefficient, _x_type, _y_type,_rootid);

        Response.Write(resultJson);

    }
}
